package com.ldf.jrtsp.handler;

import cn.hutool.core.collection.CollectionUtil;
import com.ldf.jrtsp.parser.RtspDataParser;
import com.ldf.jrtsp.player.RtspPlayer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

import static com.ldf.jrtsp.constants.RtspConstants.RTSP_OK;
import static com.ldf.jrtsp.enums.RtspOptionsType.OPTIONS;

/**
 * option reply handle
 *
 * @author lidaofu
 * @since 2024/6/19
 **/
public class RtspOptionReplyHandle implements RtspReplyHandle {
    private final static Logger log = LoggerFactory.getLogger(RtspOptionReplyHandle.class);

    @Override
    public void handle(RtspPlayer player, RtspDataParser rtspDataParser) {
        if (rtspDataParser.getResponse().equals(RTSP_OK)) {
            if (CollectionUtil.isEmpty(player.getOptions())) {
                List<String> optionsTypes = new ArrayList<>();
                String options = rtspDataParser.getHeaders().get("Public");
                String[] split = options.split(",");
                for (String option : split) {
                    optionsTypes.add(option.trim().toUpperCase());
                }
                player.setOptions(optionsTypes);
            }
            player.sendDescribe(null);
        } else {
            log.error("option reply error\n{}", rtspDataParser.getResponse());
        }
        player.sendOptionsCallBack(OPTIONS, rtspDataParser);
    }

}
